define([], function() {
    'use strict';
    /**
     * 给区域增加loading状态
     * 依赖于loading样式
     * @param id - 区域的id
     * @function showLoading - 显示loading
     * @function closeLoading - 关闭loading
     * @function disabled - 增加禁用遮罩层
     * @function removeDisabled - 移除禁用遮罩层
     */
    var setContainerStatus = {
        showLoading: function(id){
            this.closeLoading(id);
            $("#" + id).addClass("yby-loading-box").append('<div class="yby-loading-modal"><span class="loading"></span></div>');
        },
        closeLoading: function(id){
            $("#" + id).removeClass("yby-loading-box").find(".yby-loading-modal").remove();
        },
        disabled: function(id){
            this.removeDisabled(id);
            $("#" + id).addClass("yby-loading-box").append('<div class="yby-loading-modal"></div>');
        },
        removeDisabled: function(id){
            $("#" + id).removeClass("yby-loading-box").find(".yby-loading-modal").remove();
        }
    }

    var getDateTime = function(str){
        var aDate = new Date(str);
        var str = aDate.getFullYear() + '年' + (aDate.getMonth() + 1) + '月' + aDate.getDate() + '日';
        return str;
    }

    var filterData = function(list){
        var aList = [];
        // 过滤type = 4
        aList = list.filter(function(item){
            return item.type === 4;
        })

        // name + date相同的value相加
        var obj = {};
        aList.forEach(function(item){
            var aKey = item.name + '_' + getDateTime(item.date);
            if(!obj[aKey]){
                obj[aKey] = item;
            }else{
                obj[aKey].value += item.value;
            }
        })
        var list_value = [];
        for(var aKey in obj){
            list_value.push(obj[aKey]);
        }

        list_value = list_value.sort(function(a, b){
            return b.value - a.value;
        })

        return list_value;
    }

    /**
     * 设置ajax请求的状态
     */
    var setAjaxStatus = function(options){
        var loading = {
            global: false,
            apis:{}
        };
        
    }

    /**
     * 获取菜单transfer显示需要的keys
     * 规则：如果下级一个也没有选择或者全选，并且该节点选中，则该key选中；
     * @param {*} treeData 
     * @param {*} keys 
     */
    var getMenuShowKeys = function(treeData, keys, key){
        var aKeys = [];
        var deepLoad = function(list){
            var isAll = true;
            var isSelect = false;
            list.forEach(function(item){
                var theKey = item[key];
                var flag = keys.indexOf(theKey) > -1 ? true : false;
                isAll = flag ? isAll : false;
                isSelect = flag ? true : isSelect;
                if(item.children && item.children.length > 0){
                    var res = deepLoad(item.children);
                    if((res.isAll || !res.isSelect) && flag){
                        aKeys.push(theKey);
                    }
                }else{
                    if(flag){
                        aKeys.push(theKey);
                    }
                }
            })
            return {
                isAll: isAll,
                isSelect: isSelect
            };
        }
        deepLoad(treeData);
        return aKeys;
    }

    return {
        setContainerStatus: setContainerStatus,
        filterData: filterData,
        getDateTime: getDateTime,
        getMenuShowKeys: getMenuShowKeys
    }
});